<template>
    <div class="main">
        <el-card class="box-card">
            <Form :formobj="formobj" :formdata="formData" ref="form" :formrules="formrules"></Form>
            <div style="padding-left:100px">
                <el-button type="primary" @click="submit" >确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-card>
    </div>
</template>
<script>
import Form from "@/components/form.vue"
import { contentApiAdd, columnqueryAll, contentApiedit,contentApiqueryOne } from "@/api/webpage";
import { validateNum } from "@/utils/validator";
export default {
    name: "xzzxaddform",
    components: {
        Form
    },
    data() {
        return {
            options: [],
            formData: {
                columnId:'',
                title:'',
                fileUrl:'',
                listfile:'',
            },
            formobj: [
                { label: "所属栏目", key: "columnId", type: "select",options:[], disabled:false },
                // { label: "图片", key: "url", type: "pic" },
                { label: "标题", key: "title", type: "input" },
                { label: "附件", key: "listfile", type: "execl",word:'请上传文件' },
                // { label: "作者", key: "author", type: "input" },
                // { label: "审核人", key: "postedBy", type: "input" },
                // { label: "排序", key: "no", type: "input" },
                // { label: "内容", key: "content", type: "editor" }
            ],
            formrules: {
                title: [
                    { required: true, message: "请输入标题", trigger: "blur" },
                ],
                columnId: [
                    { required: true, message: "请选择栏目", trigger: "blur" },
                ],
            },
        }
    },
    created() {
        if(this.$route.query.cid){
            this.getlist(this.$route.query.cid);
        }else{
            this.formobj[0].disabled = false
        }

        if(this.$route.query.id){
            this.getdata(this.$route.query.id)
        }


    },
    methods: {
        getdata(id){
            contentApiqueryOne({id:id}).then(res=>{
                if(res.code == 0){
                    this.formData = res.data
                }
            })
        },
        getlist(id) {
            let data = {
                name: "",
                code: "",
                parentId: id,
            };
            columnqueryAll(data).then((res) => {
                if (res.code == 0) {
                    this.options = this.formobj[0].options = res.data.map(item => {
                        return { label: item.name, value: item.id }
                    });
                }
            });
        },
        handleCurrentChange(val) {
            this.params.page = val;
        },
        submit(){
           this.$refs.form.getData().then(res=>{
             console.log('下载中心~ res', res)
             // 处理fileName
             let b = []
             let a = []
             if(res.listfile && Array.isArray(res.listfile)){
               res.fileName = ''
               res.listfile.map(it=>{
                 b.push(it.name)
                 console.log("看看存在不存在show",it.url.indexOf('/show'))
                 if(it.url.indexOf("/show") == -1){ // 不包含
                   a.push(it.url)
                 }else{
                   a.push(it.url.substring(5)) // 截取/show 以后的字符串
                 }
               })
               res.fileName = b.join(',')
               res.fileUrl = a.join(',')
             }
             console.log('下载中心~ res',res);

             if (this.$route.query.type == "edit") {
               contentApiedit(res).then((response) => {
                  if (response.data == 1) {
                    this.$message.success("成功");
                    this.$router.go(-1)
                  } else {
                    this.$message.error("失败");
                  }
                })
                .catch((err) => {
                  console.log(err);
                });
             }else{
                res.type = 'article'
                contentApiAdd(res).then((response) => {
                    if (response.data == 1) {
                        this.$message.success("成功");
                        this.$router.go(-1)
                    } else {
                        this.$message.error("失败");
                    }
                    })
                    .catch((err) => {
                    console.log(err);
                    });
                }
                })
        },
        cancel(){
            this.$router.go(-1)
        },
    }
}
</script>
<style scoped>
@import "~@/assets/css/adminstyle.css";
</style>
